﻿

using DotNetCore.CAP;
using ExampleService.APIContract;
using JwtAuth.DTO;
using Leo.Common.Logger;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace JwtAuth.Controllers
{
    [Route("api/auth")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly AuthService _authService;
        private readonly ILogs log;
        private readonly IExampleService exampleService;
        private readonly ILogs<LoginController> logs;

        public LoginController(AuthService authService, ILogs loggerService, IExampleService exampleService, ILogs<LoginController> logs)
        {
            _authService = authService;
            this.log = loggerService;
            this.exampleService = exampleService;
            this.logs = logs;
        }

        [HttpGet("test")]
        public async Task<string> getTest()
        {

            var res = await exampleService.GetUserByIdAsync(1);

            log.Error<LoginController>(res.Data.Name);

            var addres = await exampleService.AddUser(new ExampleService.Infrastructure.User());
            log.Error<LoginController>(addres.Data);

            var delres = await exampleService.DeleteUsers(1);
            log.Error<LoginController>(delres.Data);

            return "OK";
        }

        [HttpPost("login")]
        public IActionResult Login(LoginRequest request)
        {
            log.Debug<LoginController>("我是日志Debug");
            log.Info<LoginController>("我是日志Info");
            log.Warn<LoginController>("我是日志Warn");
            log.Error<LoginController>("我是日志Error");
            // 这里应该验证用户凭证，示例直接通过
            // 实际应查询数据库验证用户
            if (request.UserName != "admin" || request.Password != "123456")
            {
                return Unauthorized("Invalid credentials");
            }

            // 从数据库获取实际角色
            var token = _authService.GenerateToken(request.UserName, "Administrator");


            return Ok(new TokenResponse { Token = token });
        }

        [HttpGet]
        [Authorize(Roles = "Administrator")] // 需要管理员角色
        public async Task<IActionResult> Get()
        {
            var res = await exampleService.GetUserByIdAsync(1);
            await exampleService.DeleteUsers(1);
            log.Error<LoginController>(res.Data.Name);

            return Ok("OK");
        }


        [HttpGet("token/test")]
        [Authorize(Roles = "Administrator")] // 需要管理员角色
        public IActionResult GetTokenTest()
        {
            return Ok("token-test");
        }

        [HttpGet("cap/message")]
        public async Task<IActionResult> GetCAPTestMessage(ICapPublisher publisher)
        {
            await publisher.PublishAsync("test.message", new { Name = "CAP Test Message", Time = DateTime.Now });
            Console.WriteLine("publish cap");
            return Ok("OK");
        }


        [HttpGet("logtest")]
        public string GetLogTest()
        {
            logs.Info("xxxxx--info");
            logs.Warn("xxxxx--warn");
            logs.Error("xxxxx--error");

            return "ok";
        }


    }
}
